package com.jiuyin.dianjing.ui.activity.matchInfo;


import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;
import com.jiuyin.dianjing.R;
import com.jiuyin.dianjing.ui.activity.base.BaseActivity;
import com.jiuyin.dianjing.ui.activity.team.TeamApplyListActivity;
import com.jiuyin.dianjing.ui.fragment.match.FragmentMatchGameData;
import com.jiuyin.dianjing.ui.fragment.match.FragmentMatchRanking;
import com.jiuyin.dianjing.ui.fragment.match.FragmentMatchRecruitmentInformation;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_APPLY_LIST;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_COMPETITIONID;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_COMPET_ID;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_IS_CREATOR;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_NAME;

/**
 * 赛事详情界面
 */
public class MatchInfoDetailActivity extends BaseActivity {
    private final List<Fragment> mListFragment = new ArrayList<>();
    @BindView(R.id.vp_view_container)
    ViewPager mViewPager;
    @BindView(R.id.tabs)
    TabLayout mTabLayout;
    private FragmentPagerAdapter mSectionsPagerAdapter;
    private String competitionId;
    private String isCreator;
    private static final String CREATOR = "1";

    @Override
    protected int setLayoutId() {
        return R.layout.activity_match_info_detail;
    }

    @Override
    protected void initData() {
    }


    @Override
    protected void initView() {
        setTitle(getIntent().getStringExtra(KEY_NAME));
        isCreator = getIntent().getStringExtra(KEY_IS_CREATOR);
        competitionId = getIntent().getStringExtra(KEY_COMPET_ID);
        if (CREATOR.equals(isCreator)) {
            setRightButton(R.drawable.img_manager, v -> applyMatch());
        }
        mListFragment.add(FragmentMatchRecruitmentInformation.newInstance(competitionId));
        mListFragment.add(FragmentMatchGameData.newInstance(competitionId, isCreator));
        mListFragment.add(FragmentMatchRanking.newInstance(competitionId));
        mSectionsPagerAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public int getCount() {
                return mListFragment.size();
            }

            @NonNull
            @Override
            public Fragment getItem(int position) {
                return mListFragment.get(position);
            }

            @Nullable
            @Override
            public CharSequence getPageTitle(int position) {
                switch (position) {
                    case 0:
                        return getString(R.string.match_recruitment_information);
                    case 1:
                        return getString(R.string.match_game_data);
                    default:
                        return getString(R.string.match_ranked);
                }
            }
        };
        mViewPager.setAdapter(mSectionsPagerAdapter);
        mViewPager.setOffscreenPageLimit(mListFragment.size());
        mTabLayout.setupWithViewPager(mViewPager);
        mTabLayout.getTabAt(0).setIcon(getDrawable(R.drawable.match_detail_recruitment_information));
        mTabLayout.getTabAt(1).setIcon(getDrawable(R.drawable.match_detail_game_data));
        mTabLayout.getTabAt(2).setIcon(getDrawable(R.drawable.match_detail_ranked));
        mTabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));
    }

    /**
     * 批准赛事
     */
    private void applyMatch() {
        Bundle bundle = new Bundle();
        bundle.putString(KEY_APPLY_LIST, "0");
        bundle.putString(KEY_COMPETITIONID, competitionId);
        goTargetActivity(TeamApplyListActivity.class, bundle);
    }
}
